<template>
  <div class="lb-customer-talking-list">
    <top-nav></top-nav>
    <div class="page-main">
      <el-row class="page-top-operate">
        <lb-button
          type="primary"
          icon="el-icon-plus"
          @click="addEditTalkingItem('add')"
          >添加话术</lb-button
        >
      </el-row>
      <div class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm"
          ref="searchForm"
        >
          <el-form-item label="话术内容" prop="content">
            <el-input
              v-model="searchForm.content"
              placeholder="请输入话术内容"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1)"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('searchForm')"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column label="创建人">
          <template slot-scope="scope">
            <span>{{ scope.row.user_id === 0 ? '管理员' : '其他' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="content" label="内容"></el-table-column>
        <el-table-column prop="type.title" label="类型"></el-table-column>
        <el-table-column prop="address" label="是否开启">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @change="updateCustomerTalkInfo(scope.row.id, scope.row.status)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                size="mini"
                plain
                type="primary"
                @click="
                  addEditTalkingItem('edit', {
                    id: scope.row.id,
                    content: scope.row.content,
                    top: scope.row.top,
                    type: scope.row.type.id
                  })
                "
                >{{ $t('action.edit') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="danger"
                @click="delCustomerTalkInfo(scope.row.id)"
                >{{ $t('action.delete') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      ></lb-page>
    </div>
    <el-dialog
      :title="dialogForm.id ? '编辑话术' : '添加话术'"
      :visible.sync="centerDialogVisible"
      width="600px"
      center
    >
      <el-form
        class="dialog-form"
        :model="dialogForm"
        :rules="dialogFormRules"
        label-width="90px"
        ref="dialogForm"
      >
        <el-form-item label="话术" prop="content">
          <el-input
            type="textarea"
            :rows="3"
            v-model="dialogForm.content"
            placeholder="请输入话术内容"
            maxlength="120"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="分类" prop="type">
          <el-select
            v-model="dialogForm.type"
            v-el-select-loadmore="loadmore"
            placeholder="请选择"
          >
            <el-option
              v-for="(item, index) in options"
              :key="index"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序值" prop="top">
          <el-input-number
            class="lb-input-number"
            v-model="dialogForm.top"
            :controls="false"
            :precision="0"
            :min="0"
            placeholder="请输入排序值"
          ></el-input-number>
          <lb-tool-tips>值越大, 排序越靠前</lb-tool-tips>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">{{
          $t('action.cancel')
        }}</el-button>
        <el-button type="primary" @click="submitFormInfo('dialogForm')">{{
          $t('action.submit')
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  directives: {
    'el-select-loadmore': {
      bind (el, binding) {
        // 获取element-ui定义好的scroll盒子
        const SELECTWRAP_DOM = el.querySelector(
          '.el-select-dropdown .el-select-dropdown__wrap'
        )
        SELECTWRAP_DOM.addEventListener('scroll', function () {
          const condition =
            this.scrollHeight - this.scrollTop <= this.clientHeight
          if (condition) {
            binding.value()
          }
        })
      }
    }
  },
  data () {
    return {
      searchForm: {
        content: '',
        limit: 10,
        page: 1,
        page_count: 10
      },
      dialogForm: {
        content: '',
        type: '',
        top: ''
      },
      dialogFormRules: {
        content: {
          required: true,
          type: 'string',
          message: '请输入话术内容',
          trigger: 'blur'
        },
        type: {
          required: true,
          type: 'number',
          message: '请选择分类',
          trigger: 'blur'
        },
        top: {
          required: true,
          type: 'number',
          message: '请输入排序值',
          trigger: 'blur'
        }
      },
      centerDialogVisible: false,
      options: [],
      loading: false,
      tableData: [],
      total: 0,
      optionsPage: {
        page: 1,
        page_count: 10,
        isLast: false
      }
    }
  },
  created () {
    this.loadmore()
    this.getTableDataList()
  },
  methods: {
    resetForm (name) {
      this.$refs[name].resetFields()
      this.getTableDataList(1)
    },
    getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      let { searchForm } = this
      searchForm.page_count = searchForm.limit
      this.$api.getCustomerTalkList(searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    addEditTalkingItem (type, formInfo) {
      this.dialogForm =
        type === 'add' ? { content: '', top: 0, type: '' } : formInfo
      this.centerDialogVisible = true
    },
    updateCustomerTalkInfo (id, status) {
      this.$api.editCustomerTalkInfo({ data: { id, status } }).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t('tips.successOper'))
        } else {
          this.getTableDataList()
        }
      })
    },
    delCustomerTalkInfo (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.$api.delCustomerTalkInfo({ id }).then(res => {
          if (res.code === 200) {
            this.$message.success(this.$t('tips.successDel'))
            this.searchForm.page =
              this.searchForm.page <
                Math.ceil((this.total - 1) / this.searchForm.limit)
                ? this.searchForm.page
                : Math.ceil((this.total - 1) / this.searchForm.limit)
            this.getTableDataList()
          }
        })
      })
    },
    submitFormInfo (name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          let { dialogForm } = this
          let urlType = dialogForm.id
            ? 'editCustomerTalkInfo'
            : 'addCustomerTalkInfo'
          this.$api[urlType]({ data: dialogForm }).then(res => {
            if (res.code === 200) {
              this.$message.success(this.$t('tips.successSub'))
              this.centerDialogVisible = false
              this.getTableDataList()
            }
          })
        }
      })
    },
    loadmore () {
      let { optionsPage } = this
      if (!optionsPage.isLast) {
        this.$api.getCustomerTalkClassifyList(optionsPage).then(res => {
          console.log(res)
          if (res.code === 200) {
            this.options.push(...res.data.data)
            console.log(this.options)
            this.optionsPage.isLast =
              this.optionsPage.page === res.data.total_page
            this.optionsPage.page += 1
          }
        })
      }
    },
    getListData () { },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-customer-talking-list {
  width: 100%;
  .page-main {
    width: 100%;
  }
  .el-input {
    width: 200px;
  }
  .dialog-form {
    .el-input,
    .el-select,
    .el-input-number {
      width: 300px;
    }
    .el-textarea {
      width: 400px;
    }
  }
}
</style>
